package com.asiainfo.dacp.datastash.broker.schedule.buildinjob;


import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogVo;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogger;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLoggerService;
import com.asiainfo.dacp.datastash.broker.util.LeaderElection;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 增加日志表的分区
 *
 * @author MeiKefu
 * @date 2017-11-30
 */
public class StashTraceLogCrtTableJob implements Job {

    @Autowired
    private StashTraceLogger tracelog;
    private String clzName = this.getClass().getSimpleName();

    @Autowired
    private StashTraceLoggerService stashTraceLoggerService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //实现选主，由主来完成这个服务的调用
        try {
            new LeaderElection(new LeaderSelectorListenerAdapter(){
                public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
                    tracelog.debug(new StashTraceLogVo(clzName,"traceCrtTabJob:election","选主成功"));
                    stashTraceLoggerService.createCdrTable();
                }
            }).startup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
